
package com.xiaoma.myaudience.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * <br/>
 * 文件工具类. <br/>
 * 用于存取文件,判断文件是否存在等作用
 * 
 * @author wjying
 */
public class FileUtils {
    /**
     * 删除目录或者文件
     * 
     * @param path
     * @return
     */
    public static void delete(String path) {
        delete(new File(path));
    }

    /**
     * 删除目录或者文件
     * 
     * @param file
     */
    public static void delete(File file) {
        if (file != null && file.exists()) {
            if (file.isDirectory()) {
                File[] subs = file.listFiles();
                if (subs != null) {
                    for (File sub : subs) {
                        delete(sub.toString());
                    }
                }
            } else {
                deleteFile(file);
            }
        }
    }

    /**
     * 删除单个文件
     * 
     * @param path
     */
    public static void deleteFile(String path) {
        delete(new File(path));
    }

    /**
     * 删除单个文件
     * 
     * @param file
     */
    public static void deleteFile(File file) {
        if (file != null && file.exists()) {
            file.delete();
        }
    }

    /**
     * 创建文件<br/>
     * 如果原来文件存在,先删除
     * 
     * @param path
     */
    public static void createFile(String path) {
        createFile(new File(path));

    }

    /**
     * 创建文件<br/>
     * 如果原来文件存在,先删除
     * 
     * @param file
     */
    public static void createFile(File file) {
        if (file.exists()) {
            file.delete();
        }

        File parent = file.getParentFile();
        if (!parent.exists()) {
            parent.mkdirs();
        }

        try {
            file.createNewFile();
        } catch (IOException e) {
        }

    }

    /**
     * 拷贝文件
     * 
     * @param srcFile
     * @param destFile
     * @return
     */
    public static boolean copyFile(File srcFile, File destFile) {
        boolean result = false;
        try {
            InputStream in = new FileInputStream(srcFile);
            try {
                result = copyToFile(in, destFile);
            } finally {
                in.close();
            }
        } catch (IOException e) {
            result = false;
        }
        return result;
    }

    /**
     * 拷贝文件
     * 
     * @param inputStream
     * @param destFile
     * @return
     */
    public static boolean copyToFile(InputStream inputStream, File destFile) {
        try {
            if (destFile.exists()) {
                destFile.delete();
            }
            OutputStream out = new FileOutputStream(destFile);
            try {
                byte[] buffer = new byte[4096];
                int bytesRead;
                while ((bytesRead = inputStream.read(buffer)) >= 0) {
                    out.write(buffer, 0, bytesRead);
                }
            } finally {
                out.close();
            }
            return true;
        } catch (IOException e) {
            return false;
        }
    }

    // /**
    // * Read a text file into a String, optionally limiting the length.
    // *
    // * @param file to read (will not seek, so things like /proc files are OK)
    // * @param max length (positive for head, negative of tail, 0 for no limit)
    // * @param ellipsis to add of the file was truncated (can be null)
    // * @return the contents of the file, possibly truncated
    // * @throws IOException if something goes wrong reading the file
    // */
    // public static String readTextFile(File file, int max, String ellipsis)
    // throws IOException {
    // InputStream input = new FileInputStream(file);
    // try {
    // long size = file.length();
    // if (max > 0 || (size > 0 && max == 0)) { // "head" mode: read the
    // // first N bytes
    // if (size > 0 && (max == 0 || size < max))
    // max = (int) size;
    // byte[] data = new byte[max + 1];
    // int length = input.read(data);
    // if (length <= 0)
    // return "";
    // if (length <= max)
    // return new String(data, 0, length);
    // if (ellipsis == null)
    // return new String(data, 0, max);
    // return new String(data, 0, max) + ellipsis;
    // } else if (max < 0) { // "tail" mode: keep the last N
    // int len;
    // boolean rolled = false;
    // byte[] last = null, data = null;
    // do {
    // if (last != null)
    // rolled = true;
    // byte[] tmp = last;
    // last = data;
    // data = tmp;
    // if (data == null)
    // data = new byte[-max];
    // len = input.read(data);
    // } while (len == data.length);
    //
    // if (last == null && len <= 0)
    // return "";
    // if (last == null)
    // return new String(data, 0, len);
    // if (len > 0) {
    // rolled = true;
    // System.arraycopy(last, len, last, 0, last.length - len);
    // System.arraycopy(data, 0, last, last.length - len, len);
    // }
    // if (ellipsis == null || !rolled)
    // return new String(last);
    // return ellipsis + new String(last);
    // } else { // "cat" mode: size unknown, read it all in streaming
    // // fashion
    // ByteArrayOutputStream contents = new ByteArrayOutputStream();
    // int len;
    // byte[] data = new byte[1024];
    // do {
    // len = input.read(data);
    // if (len > 0)
    // contents.write(data, 0, len);
    // } while (len == data.length);
    // return contents.toString();
    // }
    // } finally {
    // input.close();
    // }
    // }
}
